package com.qdsgjsfk.vision.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qdsgjsfk.vision.BR;
import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.databinding.ItemCollectClassBinding;
import com.qdsgjsfk.vision.databinding.ItemLetterBinding;
import com.rest.response.CollectClassResponse;

import java.util.List;

import com.qdsgjsfk.vision.ui.CollectStudentListActivity;


/**
 * Created by HP on 2019/10/31.
 */

public class CollectClassAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<CollectClassResponse.Class> getList() {
        return list;
    }

    public void setList(List<CollectClassResponse.Class> list) {
        this.list = list;
    }

    private List<CollectClassResponse.Class> list;

    private Context context;

    private String taskId;

    public CollectClassAdapter(List<CollectClassResponse.Class> list,String taskId, Context context) {
        this.list = list;
        this.context = context;
        this.taskId = taskId;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == 1){
            ItemCollectClassBinding itemCollectClassBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_collect_class, parent, false);
            return new CollectClassHolder(itemCollectClassBinding);
        }else{
            ItemLetterBinding itemLetterBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_letter, parent, false);
            return new GradeHolder(itemLetterBinding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof CollectClassHolder){
            ItemCollectClassBinding itemCollectClassBinding = ((CollectClassHolder) holder).getItemCollectClassBinding();
            itemCollectClassBinding.setVariable( BR.classItem, list.get(position));
            if (list.get(position - 1).type == 0) {
                if (list.size() <= position + 1) {
                    itemCollectClassBinding.container.setBackgroundResource(R.drawable.round_white);
                    itemCollectClassBinding.line.setVisibility(View.GONE);
                } else {
                    if (list.get(position + 1).type == 0) {
                        itemCollectClassBinding.container.setBackgroundResource(R.drawable.round_white);
                        itemCollectClassBinding.line.setVisibility(View.GONE);
                    } else {
                        itemCollectClassBinding.container.setBackgroundResource(R.drawable.round_white_top);
                        itemCollectClassBinding.line.setVisibility(View.VISIBLE);
                    }
                }
            } else {
                if (list.size() <= position + 1) {
                    itemCollectClassBinding.container.setBackgroundResource(R.drawable.round_white_bottom);
                    itemCollectClassBinding.line.setVisibility(View.GONE);
                } else {
                    if (list.get(position + 1).type == 0) {
                        itemCollectClassBinding.container.setBackgroundResource(R.drawable.round_white_bottom);
                        itemCollectClassBinding.line.setVisibility(View.GONE);
                    } else {
                        itemCollectClassBinding.container.setBackgroundResource(R.color.white);
                        itemCollectClassBinding.line.setVisibility(View.VISIBLE);
                    }
                }
            }
          holder.itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent mIntent = new Intent(context, CollectStudentListActivity.class);
                  mIntent.putExtra("classId",list.get(position).classId);
                  mIntent.putExtra("taskId",taskId);
                  context.startActivity(mIntent);
              }
          });
        }else if(holder instanceof GradeHolder){
            ItemLetterBinding itemLetterBinding = ((GradeHolder) holder).getItemLetterBinding();
            itemLetterBinding.setVariable(BR.classItem, list.get(position));
        }

        // 1   蓝牙   2  ip
//        itemDeviceBinding.setDeviceOnClick(new DeviceItemOnclick());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class CollectClassHolder extends RecyclerView.ViewHolder {

        ItemCollectClassBinding itemCollectClassBinding;

        public ItemCollectClassBinding getItemCollectClassBinding() {
            return itemCollectClassBinding;
        }

        public void setItemCollectClassBinding(ItemCollectClassBinding itemCollectClassBinding) {
            this.itemCollectClassBinding = itemCollectClassBinding;
        }

        public CollectClassHolder(ItemCollectClassBinding itemCollectClassBinding) {
            super(itemCollectClassBinding.getRoot());
            this.itemCollectClassBinding = itemCollectClassBinding;
        }
    }


    class GradeHolder extends RecyclerView.ViewHolder {

        ItemLetterBinding itemLetterBinding;

        public ItemLetterBinding getItemLetterBinding() {
            return itemLetterBinding;
        }

        public void setItemLetterBinding(ItemLetterBinding itemLetterBinding) {
            this.itemLetterBinding = itemLetterBinding;
        }

        public GradeHolder(ItemLetterBinding itemLetterBinding) {
            super(itemLetterBinding.getRoot());
            this.itemLetterBinding = itemLetterBinding;
        }
    }


}
